package heyuanyuan;

import java.util.Scanner;

public class Lovenumber1 {
    // 顺序栈实现
    private static class SeqStack {
        private int[] data;  // 存储栈元素的数组
        private int top;   // 栈顶指针
        private int maxSize;   // 栈的最大容量

        public SeqStack(int size) {
            maxSize = size;   // 设置栈的最大容量
            data = new int[maxSize];   // 创建存储数组
            top = -1;   // 初始时栈顶指针为-1(表示空栈)
        }

        // 判断栈是否为空
        public boolean isEmpty() {
            return top == -1;
        }

        // 判断栈是否已满0
        public boolean isFull() {
            return top == maxSize - 1;
        }

        public void push(int value) {
            if (isFull()) {
                System.out.println("栈满，无法入栈");
                return;
            }
            data[++top] = value;
        }

        public int pop() {
            if (isEmpty()) {
                System.out.println("栈空，无法出栈");
                return -1;
            }
            return data[top--];
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入一个十进制数字：");
        int decimal = scanner.nextInt();

        System.out.println("请输入想要转换为几进制的数字(2,8,16)：");
        int base = scanner.nextInt();

        if (base != 2 && base != 8 && base != 16) {
            System.out.println("不支持的进制类型！");
            return;
        }

        // 计算需要的栈大小（最坏情况下需要32位）
        SeqStack stack = new SeqStack(32);
        int num = decimal;

        // 转换为目标进制
        while (num > 0) {
            int remainder = num % base;
            stack.push(remainder);
            num = num / base;
        }

        // 输出结果
        System.out.println("转换结果：");
        while (!stack.isEmpty()) {
            int digit = stack.pop();
            if (digit < 10) {
                System.out.print(digit + " ");
            } else {
                // 10以上用字母表示
                System.out.print((char)('A' + digit - 10) + " ");
            }
        }
        System.out.println();
    }
}